<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/27 0027
 * Time: 16:18
 */

namespace App\Module\Admin\Controllers;

use App\System\Base\Repositories\BaseHolidayRepository;
use App\System\Cis\Model\CisAdmin;
use App\System\Cis\Model\CisAdminRole;
use App\System\Cis\Repositories\CisAdminRepository;
use App\System\Cis\Repositories\CisAdminRoleRepository;
use App\System\Cis\Repositories\CisModuleRepository;
use App\System\Log\Repositories\LogAdminRepository;
use App\System\Log\Repositories\LogSmsRepository;
use Illuminate\Http\Request;
use Session;
use App\System\Log\Repositories\LogRepeatedEliminationRepository;

/**
 * Class PlatformController 平台管理
 * @package App\Module\Admin\Controllers
 */
class PlatformController extends Controller
{
    /*
     * 平台管理
     */

    //管理员职务列表
    public function position(Request $request)
    {
        $page = $request['page'] ?? 1;
        $CisAdminRole = CisAdminRoleRepository::getInstance();
        $data = $CisAdminRole->getlist($page);
        return view('admin.platform.position', compact('data'));
    }

    //新增职务
    public function addPosition()
    {
        $CisModule = CisModuleRepository::getInstance();
        $modules = $CisModule->getlist();
        return view('admin.platform.addPosition', compact('modules'));
    }

    //编辑职务
    public function editPosition(Request $request)
    {
        $id = $request['id'];
        $CisAdminRole = CisAdminRoleRepository::getInstance();
        $data = $CisAdminRole->getDetail($id);
        $CisModule = CisModuleRepository::getInstance();
        $modules = $CisModule->getlist();
        $actionArr = explode(',', $data->action_list);
        return view('admin.platform.editPosition', compact('data', 'modules', 'actionArr'));
    }

    //新增、编辑职务操作
    public function ajaxEditPosition(Request $request)
    {
        if ($request->isMethod('post')) {
            //获取指定字段内容
            $thepost = $request->only('id', 'name', 'actionLists');
            $id = $thepost['id'] ?? '';
            if (!isset($thepost['actionLists'])) {
                return response()->json(['message' => '必须选择权限！', 'status' => 'FAIL', 'url' => '']);
            }
            $CisAdminRole = CisAdminRoleRepository::getInstance();
            $updata['name'] = $thepost['name'];
            $updata['action_list'] = implode(',', $thepost['actionLists']);
            if ($id) {
                $isset = CisAdminRole::where('name',$thepost['name'])->where('id','!=',$id)->first();
                if($isset){
                    return response()->json(['message' => '角色名已存在！', 'status' => 'FAIL', 'url' => '']);
                }
                $res = $CisAdminRole->editData($id, $updata);
            } else {
                $isset = CisAdminRole::where('name',$thepost['name'])->first();
                if($isset){
                    return response()->json(['message' => '角色名已存在！', 'status' => 'FAIL', 'url' => '']);
                }
                $res = $CisAdminRole->addData($updata);
            }
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/platform/position') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }

    //删除职务
    public function ajaxDelPosition(Request $request)
    {
        if ($request->isMethod('post')) {
            //获取指定字段内容
            $thepost = $request->only('id');
            $id = $thepost['id'] ?? '';
            $CisAdminRole = CisAdminRoleRepository::getInstance();
            $res = $CisAdminRole->delData($id);
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => '']);
        }
    }

    //管理员列表
    public function admin(Request $request)
    {
        $keyword = $request['keyword'] ?? '';
        $positon = $request['position'] ?? '';
        $status = $request['status'] ?? '';
        $page = $request['page'] ?? 1;
        $where = array('keyword' => $keyword, 'position' => $positon, 'status' => $status, 'page' => $page);
        $CisAdmin = CisAdminRepository::getInstance();
        $data = $CisAdmin->getlist($keyword, $positon, $status, $page);
        $CisAdminRole = CisAdminRoleRepository::getInstance();
        $roleList = $CisAdminRole->getAllList(1);
        return view('admin.platform.admin', compact('data', 'where', 'roleList'));
    }

    //新增管理员
    public function addAdmin()
    {
        $CisAdminRole = CisAdminRoleRepository::getInstance();
        $roleList = $CisAdminRole->getAllList();
        $CisMoude = CisModuleRepository::getInstance();
        $moduleList = $CisMoude->getlist();
        return view('admin.platform.addAdmin', compact('roleList', 'moduleList'));
    }

    //编辑管理员
    public function editAdmin(Request $request)
    {
        $id = $request['id'];
        $CisAdmin = CisAdminRepository::getInstance();
        $data = $CisAdmin->getDetail($id);
        $CisAdminRole = CisAdminRoleRepository::getInstance();
        $roleList = $CisAdminRole->getAllList();
        $CisMoude = CisModuleRepository::getInstance();
        $moduleList = $CisMoude->getlist();
        return view('admin.platform.editAdmin', compact('data', 'roleList', 'moduleList'));
    }

    //ajax新增、编辑管理员
    public function ajaxEditAdmin(Request $request)
    {
        if ($request->isMethod('post')) {
            //获取指定字段内容
            $thepost = $request->only('id', 'id_name', 'mobile', 'name', 'password','repassword', 'admin_type_id');
            $id = $thepost['id'] ?? '';
            $CisAdmin = CisAdminRepository::getInstance();
            $updata['admin_name'] = $thepost['name'];
            //$updata['mobile'] = $thepost['mobile'];
            //$updata['name'] = $thepost['name'];
            if ($thepost['password']) {
                if($thepost['password'] != $thepost['repassword']){
                    return response()->json(['message'=>'两次密码不一致！','status'=>'FAIL','url'=>'']);
                    die;
                }
                $updata['password'] = $thepost['password'];
            }
            if(!$thepost['admin_type_id']){
                return response()->json(['message'=>'未选择职务！','status'=>'FAIL','url'=>'']);
            }
            $updata['role_id'] = $thepost['admin_type_id'];
            if ($id) {
                $isset = CisAdmin::withTrashed()->where('admin_name',$thepost['name'])->where('id','!=',$id)->first();
                if($isset){
                    return response()->json(['message' => '登录帐号已被使用！', 'status' => 'FAIL', 'url' => '']);
                }
                $res = $CisAdmin->editData($id, $updata);
            } else {
                $isset = CisAdmin::withTrashed()->where('admin_name',$thepost['name'])->first();
                if($isset){
                    return response()->json(['message' => '登录帐号已被使用！', 'status' => 'FAIL', 'url' => '']);
                }
                $updata['status'] = 0;
                $updata['is_super'] = 0;
                $res = $CisAdmin->addData($updata);
            }
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/platform/admin') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }

    //启用、禁用管理员
    public function ajaxChangeStatus(Request $request)
    {
        if ($request->isMethod('post')) {
            //获取指定字段内容
            $thepost = $request->only('id', 'status');
            $id = $thepost['id'] ?? '';
            $CisAdmin = CisAdminRepository::getInstance();
            $updata['status'] = ($thepost['status'] == 0) ? 1 : 0;
            $res = $CisAdmin->editData($id, $updata);
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => '']);
        }
    }

    //删除管理员
    public function ajaxDelAdmin(Request $request)
    {
        if ($request->isMethod('post')) {
            //获取指定字段内容
            $thepost = $request->only('id');
            $id = $thepost['id'] ?? '';
            $CisAdmin = CisAdminRepository::getInstance();
            $res = $CisAdmin->delData($id);
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => '']);
        }
    }

    //复消日志
    public function reSaleLog(Request $request)
    {
        $keyword = $request['keyword']??'';
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $where = ['keyword'=>$keyword,'start_time'=>$startTime,'end_time'=>$endTime];
        $logRepeatedElimination = LogRepeatedEliminationRepository::getInstance();
        $data = $logRepeatedElimination->getList($keyword,$startTime,$endTime);
        return view('admin.platform.reSaleLog',compact('data','where'));
    }

    //管理员日志
    public function adminLog(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $where = ['start_time'=>$startTime,'end_time'=>$endTime];
        $logAdmin = LogAdminRepository::getInstance();
        $data = $logAdmin->getList($startTime,$endTime);
        return view('admin.platform.adminLog',compact('data','where'));
    }

    //短信日志
    public function messageLog(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $where = ['start_time'=>$startTime,'end_time'=>$endTime];
        $logSms = LogSmsRepository::getInstance();
        $data = $logSms->getList($startTime,$endTime);
        return view('admin.platform.messageLog',compact('data','where'));
    }

    //ajax获取职务的权限模块
    public function ajaxGetModules(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('id');
            $id = $thepost['id'];
            $CisAdminRole = CisAdminRoleRepository::getInstance();
            $data = $CisAdminRole->getModules($id);
            return json_encode($data);
        }
    }

    //登录
    public function login()
    {
        return view('admin.platform.login');
    }

    //退出
    public function laout()
    {
        Session::put('name', '');
        Session::put('adminid', '');
        \Session::put('nickname', '');
        Session::put('authModule', '');
        return view('admin.platform.login');
    }

    //修改密码
    public function changePassword()
    {
        return view('admin.platform.changePassword');
    }

    //修改密码ajax
    public function ajaxChangePwd(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('oldpassword', 'password', 'repassword');
            if ($thepost['password'] != $thepost['repassword']) {
                return response()->json(['message' => '两次输入密码不一致！', 'status' => 'FAIL', 'url' => '']);
            } else {
                $updata['oldPwd'] = $thepost['oldpassword'];
                $updata['pwd'] = $thepost['password'];
                $CisAdmin = CisAdminRepository::getInstance();
                $id = Session::get('adminid');
                $res = $CisAdmin->changePwd($id, $updata);
                $msg = $res['message'] ?? '操作失败';
                $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
                $url = !empty($res['data']) ? url('/admin/platform/login') : '';
                return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
            }
        }
    }

    //用户登陆ajax
    public function ajaxLogin(Request $request)
    {
        $userInput = $request->get('checkCode');
        $username = $request->get('name');
        $pwd = $request->get('password');
        if ($userInput && Session::get('captcha') != $userInput) {
            return response()->json(['message' => '验证码不正确', 'status' => 'FAIL', 'url' => '']);
        } else {
            $CisAdmin = CisAdminRepository::getInstance();
            $ret = $CisAdmin->login($username, $pwd);
        }
        $url = '';
        if (!empty($ret['data'])) {
            $res = $CisAdmin->getAuth(Session::get('adminid'));
            if ($res == 'all') {
                $url = route('admin.index.index');
            } else {
                $url = route($res[0]);
            }
        }
        $msg = !empty($ret['message']) ? $ret['message'] : '登录失败';
        $status = !empty($ret['status']) ? $ret['status'] : 'FAIL';
        return response()->json(['message' => $msg, 'status' => $status, 'url' => $url]);
    }

    //忘记密码
    public function forgetPassword()
    {
        return view('admin.platform.forgetPassword');
    }

    /*
     * 方便开发时新增、编辑权限模块，上线隐藏
     */
    public function module(Request $request)
    {
        $keyword = $request['keyword'] ?? '';
        $where = array('keyword' => $keyword);
        $CisModule = CisModuleRepository::getInstance();
        $data = $CisModule->getList($keyword);
        return view('admin.platform.module', compact('data', 'where'));
    }

    public function addModule()
    {
        $CisModule = CisModuleRepository::getInstance();
        $data = $CisModule->getlist();
        return view('admin.platform.addModule', compact('data'));
    }

    public function editModule(Request $request)
    {
        $id = $request['id'];
        $CisModule = CisModuleRepository::getInstance();
        $info = $CisModule->getDetail($id);
        $data = $CisModule->getlist();
        return view('admin.platform.editModule', compact('info', 'data'));
    }

    public function ajaxModule(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('id', 'parent_id', 'name', 'action_code');
            $id = $thepost['id'] ?? '';
            $updata['parent_id'] = $thepost['parent_id'];
            $updata['name'] = $thepost['name'];
            $updata['action_code'] = $thepost['action_code'];
            $updata['type'] = 1;
            $CisModule = CisModuleRepository::getInstance();
            if ($id) {
                $res = $CisModule->editData($id, $updata);
            } else {
                $updata['created_at'] = date('Y-m-d H:i:s', time());
                $res = $CisModule->addData($updata);
            }
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/platform/module') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }

    public function delModule(Request $request)
    {
        if ($request->isMethod('post')) {
            $thepost = $request->only('id');
            $id = $thepost['id'] ?? '';
            $CisModule = CisModuleRepository::getInstance();
            $res = $CisModule->delData($id);
            $msg = $res['message'] ?? '操作失败';
            $state = !empty($res['data']) ? 'SUCCESS' : 'FAIL';
            $url = !empty($res['data']) ? url('/admin/platform/module') : '';
            return response()->json(['message' => $msg, 'status' => $state, 'url' => $url]);
        }
    }

    public function holiday(Request $request)
    {
        $holidayModule = BaseHolidayRepository::getInstance();
        $data = $holidayModule->getlist();
        return view('admin.platform.holiday', compact('data'));
    }
    public function delHoliday(Request $request)
    {
        if ($request->isMethod('post')) {
            $id = $request->only('id');

            $Module = BaseHolidayRepository::getInstance();
            $res = 0;
            if ($id)
            {
                $res = $Module->deleteData($id);
            }
            $msg = $res ? '操作成功' : '操作失败';
            $state = $res ? 'SUCCESS' : 'FAIL';
           // $url = $res ? url('/admin/platform/holiday') : '';
            return response()->json(['message' => $msg, 'status' => $state]);
        }
    }

    /**
     * 添加节假日
     */
    public function addHoliday(Request $request)
    {
        if ($request->isMethod('post'))
        {
            $data = $request->only('holiday');
            $data['created_at'] = date("Y-m-d H:i:s");
            $data['admin_id'] = Session::get('adminid');
            BaseHolidayRepository::getInstance()->addData($data);
            return response()->json(['message' => '操作成功', 'status' => 'SUCCESS','url'=>'/admin/platform/holiday']);
        }
    }
}